package com.wgj.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wgj.android.R;
import com.wgj.android.entity.Message;
import com.wgj.android.entity.NewHistory;
import com.wgj.android.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/11.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> implements View.OnClickListener {
    private List<Message> messages;
    private Context context;
    private LayoutInflater inflater;
    public  MessageAdapter(Context context ,List messages){
        this.context = context;
        if(messages==null){
            messages = new ArrayList<Message>();
        }
        this.messages=messages;
        this.inflater = LayoutInflater.from(context);

    }

    private OnItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    public void setOnItemClickListener(MessageAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    @Override
    public MessageAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_message,null);
        view.setOnClickListener(this);
        return new MessageAdapter.MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MessageAdapter.MyViewHolder holder, int position) {
        if(messages.get(position).getType()==0){
            holder.type.setText("系统消息");
        }else if(messages.get(position).getType()==1){
            holder.type.setText("分享,公排的消息");
        }else if(messages.get(position).getType()==2){
            holder.type.setText("订单,提现的消息");
        }else if(messages.get(position).getType()==3){
            holder.type.setText("生男女的消息");
        }

        holder.content.setText(messages.get(position).getContent());
        holder.time.setText( DateUtils.dateForm(messages.get(position).getCreateTime()));
        holder.itemView.setTag(position);

    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    public class MyViewHolder extends  RecyclerView.ViewHolder{
        private TextView type;
        private TextView content;
        private TextView time;

        public MyViewHolder(View view) {
            super(view);
            type = view.findViewById(R.id.type);
            content = view.findViewById(R.id.content);
            time = view.findViewById(R.id.time);
        }

    }
}
